package com.cnyz.service.impl;

import com.cnyz.base.PageResponse;
import com.cnyz.dto.CourseFileDto;
import com.cnyz.entity.CourseFile;
import com.cnyz.exception.BaseException;
import com.cnyz.mapper.CourseFileMapper;
import com.cnyz.service.CourseFileService;
import com.cnyz.service.FileUploadService;
import com.cnyz.vo.CourseFileVo;
import com.cnyz.vo.UploadVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.cnyz.utils.FileUtil.urlToLocalPath;

@Service

public class CourseFileServiceImp implements CourseFileService {

    @Autowired
    private FileUploadService fileUploadService;//文件上传服务

    @Autowired
    private CourseFileMapper courseFileMapper;

    @Override
    public void addCourseFile(CourseFileDto courseFileDto) {
        UploadVo uploadVo = fileUploadService.uploadFile(courseFileDto.getMultipartFile());
        CourseFile courseFile = CourseFile.builder()
                .name(courseFileDto.getName())
                .course(courseFileDto.getCourse())
                .description(courseFileDto.getDescription())
                .path(uploadVo.getPath())
                .type(uploadVo.getType())
                .text(uploadVo.getText())
                .description(courseFileDto.getDescription())
                .build();
        courseFileMapper.insertCourseFile(courseFile);



    }

    @Override
    public PageResponse<CourseFileVo> getCourseFilePage(Integer pageNum, Integer pageSize,Long course, String name) {

        PageHelper.startPage(pageNum, pageSize);
        List<CourseFileVo> courseFileVos = courseFileMapper.getCourseFilePage(course, name);
        Page<CourseFileVo> pages = (Page<CourseFileVo>) courseFileVos;
        return PageResponse.of(pages, CourseFileVo.class);
    }

    @Override
    public void deleteCourseFile(Long id) {
        courseFileMapper.deleteCourseFile(id);
    }

    @Override
    public void updateCourseFile(CourseFileDto courseFileDto) {
        UploadVo uploadVo = fileUploadService.uploadFile(courseFileDto.getMultipartFile());
        CourseFile courseFile = CourseFile.builder()
                .name(courseFileDto.getName())
                .description(courseFileDto.getDescription())
                .path(uploadVo.getPath())
                .type(uploadVo.getType())
                .build();
        courseFile.setId(courseFileDto.getId());
        System.out.print("更新文件 " + courseFile.toString());
        courseFileMapper.updateCourseFile(courseFile);
    }


    @Override
    public CourseFileVo getCourseFileById(Long id) {
        CourseFileVo courseFileVo = courseFileMapper.getCourseFileById(id);
        if (courseFileVo != null) {
            return courseFileVo;
        } else {
            throw new BaseException("课程文件不存在");
        }
    }

    @Override
    public void updateCourseFileText(CourseFileDto courseFileDto) {

        CourseFile file =CourseFile.builder()
                .text(courseFileDto.getText())
                .build();
        file.setId(courseFileDto.getId());
        //组装好后更新
        courseFileMapper.updateCourseFileText(file);
    }



}

